package logic

import (
	"context"
	"fmt"
	"yktwo/models/mysql"

	"yktwo/yktworpc/internal/svc"
	"yktwo/yktworpc/yktworpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *ProductAddLogic) ProductAdd(in *yktworpc.ProductAddRequest) (*yktworpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.Product
	mysql.DB.Debug().Model(&user).Where("producename = ?", in.ProduceName).Find(&user)
	product := mysql.Product{
		ProduceCode: in.ProduceCode,
		Img:         in.Img,
		ProduceName: in.ProduceName,
		Price:       float64(in.Price),
		Store:       int(in.Store),
		Status:      int(in.Status),
		UserId:      int(in.UserId),
	}
	err := mysql.DB.Debug().Model(&product).Create(&product).Error
	if err != nil {
		return nil, fmt.Errorf("创建失败")
	}
	return &yktworpc.ProductAddResponse{Id: int64(product.ID)}, nil
}
